<?php

class home extends MY_Controller {

    private $fname = '';
    private $lname = '';
    private $nname = '';
    private $status = '';

    public function __construct() {
        parent::__construct();
        $this->load->model('driver_model');
        $this->load->model('catalog');
    }

    public function index() {

        $this->session->unset_userdata('search_fname');
        $this->session->unset_userdata('search_lname');
        $this->session->unset_userdata('search_nname');
        $this->session->unset_userdata('search_status');

        $config['base_url'] = base_url() . "/driver/home/search";
        $config['total_rows'] = $this->driver_model->count();
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $utility = new Config_form();
        $config = $utility->form_pagination($config);
        $this->pagination->initialize($config);

        $result = $this->driver_model->search_list_driver();
        $this->data['result_list'] = $result;
        $this->data['num_list'] = $this->uri->segment(4);

        $this->_set_form();
        $this->load->view('driver/index', $this->data);
    }

    private function _set_form() {
        $this->data['txt_fname'] = form_input('txtFname', $this->fname, 'class="form-control" placeholder="ชื่อ"');
        $this->data['txt_lname'] = form_input('txtLname', $this->lname, 'class="form-control" placeholder="นามสกุล"');
        $this->data['txt_nname'] = form_input('txtNname', $this->nname, 'class="form-control" placeholder="ชื่อเล่น"');
        $this->data['cmb_status'] = form_dropdown('cmbStatus', $this->catalog->list_driver_status(), $this->status, 'class="form-control"');
    }

    public function search() {
        //Clean Session for searching and it come from search btn.
        if ($this->input->post('action') == 'search') {
            $this->session->unset_userdata('search_fname');
            $this->session->unset_userdata('search_lname');
            $this->session->unset_userdata('search_nname');
            $this->session->unset_userdata('search_status');

            $this->fname = $this->input->post('txtFname');
            $this->lname = $this->input->post('txtLname');
            $this->nname = $this->input->post('txtNname');
            $this->status = $this->input->post('cmbStatus');

            $this->session->set_userdata('search_fname', $this->fname);
            $this->session->set_userdata('search_lname', $this->lname);
            $this->session->set_userdata('search_nname', $this->nname);
            $this->session->set_userdata('search_status', $this->status);
        } else {
            //set value for pagination.
            $this->fname = $this->session->userdata('search_fname');
            $this->lname = $this->session->userdata('search_lname');
            $this->nname = $this->session->userdata('search_nname');
            $this->status = $this->session->userdata('search_status');
        }


        $config['base_url'] = base_url() . "/driver/home/search";
        $config['total_rows'] = $this->driver_model->count($this->fname, $this->lname, $this->nname, $this->status);
        $config['per_page'] = 10;
        $config["uri_segment"] = 4;
        $utility = new Config_form();
        $config = $utility->form_pagination($config);
        $this->pagination->initialize($config);

        $result = $this->driver_model->search_list_driver($this->fname, $this->lname, $this->nname, $this->status, $config['per_page'], $this->uri->segment(4));
        $this->data['result_list'] = $result;
        $this->data['num_list'] = $this->uri->segment(4);
        $this->_set_form();
        $this->load->view('driver/index', $this->data);
    }

}
